using Avalonia;
using Avalonia.Controls;
using Avalonia.Controls.Primitives;
using Avalonia.Interactivity;
using Avalonia.Markup.Xaml;
using Newtonsoft.Json.Linq;
using SocketTool.Views.DialogWindows;

namespace SocketTool.Views.Pages
{
    public partial class TCPClientView : UserControl
    {
        public TCPClientView()
        {
            InitializeComponent();
        }

        private void InitializeComponent()
        {
            AvaloniaXamlLoader.Load(this);
        }

        private ViewModels.TCP.TCPClientViewModel mDtContext;

        public ViewModels.TCP.TCPClientViewModel DtContext
        {
            get
            {
                if (mDtContext == null)
                    mDtContext = this.DataContext as ViewModels.TCP.TCPClientViewModel;
                return this.mDtContext;
            }
        }

        private void TextBox_OnPastingFromClipboard(object? sender, RoutedEventArgs e)
        {
            // var text = Application.Current.Clipboard.GetTextAsync().Result;
            // if(string.IsNullOrEmpty(text))
            //     return;
            // var jObj = JObject.Parse(text);
            // this.DtContext.ReqParamText = jObj.ToString();
            // e.Handled = true;
        }

        /// <summary>
        /// 添加新客户端连接
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void Button_OnClick(object? sender, RoutedEventArgs e)
        {
            AddNewSocketWindow addNewSocketWindow = new AddNewSocketWindow();
            addNewSocketWindow.ShowDialog(App.MainWindow);
        }

        /// <summary>
        /// 数据来源的互斥选择按钮
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void ToggleButton_OnChecked(object? sender, RoutedEventArgs e)
        {
            if (sender is ToggleButton toggleButton)
            {
                if (toggleButton.Name == "HistoryBtn")
                    this.FindControl<ToggleButton>("ProtocolBtn").IsChecked = false;
                else
                    this.FindControl<ToggleButton>("HistoryBtn").IsChecked = false;
            }
        }
    }
}